package com.cn.springcloudwechat.controller.personalcenter;

import com.cn.springcloudwechat.common.ajax.AjaxRes;
import com.cn.springcloudwechat.common.utils.Const;
import com.cn.springcloudwechat.common.utils.ThreadLocalUtil;
import com.cn.springcloudwechat.controller.vaccine.log.LogInfo;
import com.cn.springcloudwechat.controller.vaccine.log.OperateType;
import com.cn.springcloudwechat.model.YmUser;
import com.cn.springcloudwechat.model.YmUserChild;
import com.cn.springcloudwechat.model.dto.YmChildDto;
import com.cn.springcloudwechat.redis.RedisUtil;
import com.cn.springcloudwechat.service.personalcenter.service.YmUserChildService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/web/userChild/")
public class UserChildController{

    @Autowired
    YmUserChildService ymUserChildService;

    /**
     * 获取用户宝宝（前后台通用）
     * @param ajaxRes
     * @return
     */
    @RequestMapping("getUserChild")
    @LogInfo(operateType = OperateType.QUERY, content = "获取用户宝宝")
    public AjaxRes getUserChild(AjaxRes ajaxRes,Long userId){
        try{
            if(userId == null){
                userId = ThreadLocalUtil.getUser().getUserId();
            }
            List<YmUserChild> childList = ymUserChildService.getUserChild(userId);
            ajaxRes.setSucceed(childList, Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 新增宝宝
     * @param ajaxRes
     * @param ymUserChild
     * @return
     */
    @RequestMapping("insertUserChild")
    @LogInfo(operateType = OperateType.INSERT, content = "新增宝宝")
    public AjaxRes insertUserChild(AjaxRes ajaxRes,YmUserChild ymUserChild){
        try{
            Integer result = ymUserChildService.insertUserChild(ymUserChild);
            ajaxRes.setSucceed(result,Const.SAVE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 修改宝宝信息
     * @param ajaxRes
     * @param ymUserChild
     * @return
     */
    @RequestMapping("updateUserChild")
    @LogInfo(operateType = OperateType.UPDATE, content = "修改宝宝信息")
    public AjaxRes updateUserChild(AjaxRes ajaxRes,YmUserChild ymUserChild){
        try{
            ymUserChild.setUserId(ThreadLocalUtil.getUser().getUserId());
            Integer result = ymUserChildService.updateUserChild(ymUserChild);
            ajaxRes.setSucceed(result,Const.SAVE_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 获取用户孩子列表下拉
     * @param ajaxRes
     * @return
     */
    @RequestMapping("getChildName")
    @LogInfo(operateType = OperateType.QUERY, content = "获取用户孩子列表下拉")
    public AjaxRes getChildName(AjaxRes ajaxRes){
        try {
            List<YmChildDto> ymChildDtoList = ymUserChildService.getChildName();
            ajaxRes.setSucceed(ymChildDtoList,Const.DATA_SUCCEED);
        }catch (Exception e){
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }
}
